//
//  LRWelcomeVC.swift
//  WeiboX
//
//  Created by admin on 04/12/2016.
//  Copyright © 2016 iOS_Objective-C. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class LRWelcomeVC: UIViewController {
	
	lazy var bg: UIImageView = UIImageView.init(image: UIImage.init(named: "ad_background"))
	
	lazy var userIcon: UIImageView = {
		let imageView = UIImageView.init(image: UIImage.init(named: "avatar_default_big"))

		if let imageUrl: String = LRUserAccountVM.sharedUserAccount.userAccount?.avatar_large {
//			imageView.sd_setImage(with: URL.init(string: imageUrl), placeholderImage: "avatar_default_big")
			imageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
		}
		
		imageView.layer.cornerRadius = 45
		imageView.layer.masksToBounds = true
		
		return imageView
	}()
	
	lazy var name: UILabel = {
		let label = UILabel()
		
		if let name = LRUserAccountVM.sharedUserAccount.userAccount?.name {
			label.text = "欢迎回来, \(name)"
		} else {
			label.text = "欢迎回来, xxx"
		}
		
		label.font = UIFont.systemFont(ofSize: 13)
		label.textColor = UIColor.darkGray
		label.textAlignment = .center
		
		label.alpha = 0
		return label
	}()
	
	override func loadView() {
		view = bg
	}
	
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
	
	override func viewDidAppear(_ animated: Bool) {
		iconAnim()
	}
	
	private func setupUI() {
		view.addSubview(userIcon)
		view.addSubview(name)
		
		userIcon.snp.makeConstraints { (make) in
			make.centerX.equalToSuperview()
			make.top.equalToSuperview().offset(300)
			make.size.equalTo(CGSize(width: 90, height: 90))
		}
		
		name.snp.makeConstraints { (make) in
			make.centerX.equalTo(userIcon)
			make.top.equalTo(userIcon.snp.bottom).offset(10)
		}
	}
	
	private func iconAnim() {
		userIcon.snp.updateConstraints { (make) in
			make.top.equalToSuperview().offset(100)
		}
		
		UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
			self.view.layoutIfNeeded()
		}) { (_) in
			UIView.animate(withDuration: 0, animations: { 
				self.name.alpha = 1
			}, completion: { (_) in
				NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotifacationName), object: nil)
			})
		}
	}
	
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
